package com.pro.core.validator;

import com.baidu.unbiz.fluentvalidator.ValidationError;
import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;

import java.util.Map;

/**
 * 校验Map不为null
 * Created by shuzheng on 2017/2/18.
 */
public class MapNotEmptyValidator extends ValidatorHandler<Map<String,Object>>
        implements Validator<Map<String,Object>> {

    private String fieldName;

    public MapNotEmptyValidator(String fieldName) {
        this.fieldName = fieldName;
    }

    @Override
    public boolean validate(ValidatorContext context, Map<String,Object> s) {
        if (null == s || s.size() == 0) {
            context.addError(ValidationError.create(String.format("%s不能为空！", fieldName))
                    .setErrorCode(-1)
                    .setField(fieldName)
                    .setInvalidValue(s));
            return false;
        }
        return true;
    }

}
